﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Data;
using Microsoft.Practices.Prism.Commands;
using ParaEditor.Common;
using ParaEditor.DAO.ParaDAO;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Domain.Entities;
using ParaEditor.Infrastructure;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(IPara3085ViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class Para3085ViewModel : ViewModelBase, IPara3085ViewModel
    {
        private Para3085DAO paramDao;

        private ICollectionView infoVerList;
        public ICollectionView InfoVerList
        {
            get
            {
                return infoVerList;
            }
            set
            {
                this.infoVerList = value;
                this.RaisePropertyChangedEvent("InfoVerList");
            }
        }

        private ICollectionView paraCollectionDetail;
        public ICollectionView ParaCollectionDetail
        {
            get
            {
                return this.paraCollectionDetail;
            }
            set
            {
                this.paraCollectionDetail = value;
                this.RaisePropertyChangedEvent("ParaCollectionDetail");
            }
        }

        private Visibility editGroupBoxVisible;
        public Visibility EditGroupBoxVisible
        {
            get
            {
                return this.editGroupBoxVisible;
            }
            set
            {
                this.editGroupBoxVisible = value;
                this.RaisePropertyChangedEvent("EditGroupBoxVisible");
            }
        }

        private ICollectionView baseScLineList;
        public ICollectionView BaseScLineList
        {
            get
            {
                return baseScLineList;
            }
            set
            {
                this.baseScLineList = value;
                this.RaisePropertyChangedEvent("BaseScLineList");
            }
        }

        private ICollectionView shadow1ScLineList;
        public ICollectionView Shadow1ScLineList
        {
            get
            {
                return shadow1ScLineList;
            }
            set
            {
                this.shadow1ScLineList = value;
                this.RaisePropertyChangedEvent("Shadow1ScLineList");
            }
        }

        private ICollectionView shadow2ScLineList;
        public ICollectionView Shadow2ScLineList
        {
            get
            {
                return shadow2ScLineList;
            }
            set
            {
                this.shadow2ScLineList = value;
                this.RaisePropertyChangedEvent("Shadow2ScLineList");
            }
        }

        private ICollectionView shadow3ScLineList;
        public ICollectionView Shadow3ScLineList
        {
            get
            {
                return shadow3ScLineList;
            }
            set
            {
                this.shadow3ScLineList = value;
                this.RaisePropertyChangedEvent("Shadow3ScLineList");
            }
        }

        private ICollectionView shadow4ScLineList;
        public ICollectionView Shadow4ScLineList
        {
            get
            {
                return shadow4ScLineList;
            }
            set
            {
                this.shadow4ScLineList = value;
                this.RaisePropertyChangedEvent("Shadow4ScLineList");
            }
        }

        private ICollectionView shadow5ScLineList;
        public ICollectionView Shadow5ScLineList
        {
            get
            {
                return shadow5ScLineList;
            }
            set
            {
                this.shadow5ScLineList = value;
                this.RaisePropertyChangedEvent("Shadow5ScLineList");
            }
        }

        private ICollectionView baseScList;
        public ICollectionView BaseScList
        {
            get
            {
                return baseScList;
            }
            set
            {
                this.baseScList = value;
                this.RaisePropertyChangedEvent("BaseScList");
            }
        }

        private ICollectionView shadow1ScList;
        public ICollectionView Shadow1ScList
        {
            get
            {
                return shadow1ScList;
            }
            set
            {
                this.shadow1ScList = value;
                this.RaisePropertyChangedEvent("Shadow1ScList");
            }
        }

        private ICollectionView shadow2ScList;
        public ICollectionView Shadow2ScList
        {
            get
            {
                return shadow2ScList;
            }
            set
            {
                this.shadow2ScList = value;
                this.RaisePropertyChangedEvent("Shadow2ScList");
            }
        }

        private ICollectionView shadow3ScList;
        public ICollectionView Shadow3ScList
        {
            get
            {
                return shadow3ScList;
            }
            set
            {
                this.shadow3ScList = value;
                this.RaisePropertyChangedEvent("Shadow3ScList");
            }
        }

        private ICollectionView shadow4ScList;
        public ICollectionView Shadow4ScList
        {
            get
            {
                return shadow4ScList;
            }
            set
            {
                this.shadow4ScList = value;
                this.RaisePropertyChangedEvent("Shadow4ScList");
            }
        }

        private ICollectionView shadow5ScList;
        public ICollectionView Shadow5ScList
        {
            get
            {
                return shadow5ScList;
            }
            set
            {
                this.shadow5ScList = value;
                this.RaisePropertyChangedEvent("Shadow5ScList");
            }
        }

        private string baseStationID;
        public string BaseStationID
        {
            get
            {
                return this.baseStationID;
            }
            set
            {
                this.baseStationID = value;
                this.RaisePropertyChangedEvent("BaseStationID");
            }
        }

        private string shadow1StationID;
        public string Shadow1StationID
        {
            get
            {
                return this.shadow1StationID;
            }
            set
            {
                this.shadow1StationID = value;
                this.RaisePropertyChangedEvent("Shadow1StationID");
            }
        }

        private string shadow2StationID;
        public string Shadow2StationID
        {
            get
            {
                return this.shadow2StationID;
            }
            set
            {
                this.shadow2StationID = value;
                this.RaisePropertyChangedEvent("Shadow2StationID");
            }
        }

        private string shadow3StationID;
        public string Shadow3StationID
        {
            get
            {
                return this.shadow3StationID;
            }
            set
            {
                this.shadow3StationID = value;
                this.RaisePropertyChangedEvent("Shadow3StationID");
            }
        }

        private string shadow4StationID;
        public string Shadow4StationID
        {
            get
            {
                return this.shadow4StationID;
            }
            set
            {
                this.shadow4StationID = value;
                this.RaisePropertyChangedEvent("Shadow4StationID");
            }
        }

        private string shadow5StationID;
        public string Shadow5StationID
        {
            get
            {
                return this.shadow5StationID;
            }
            set
            {
                this.shadow5StationID = value;
                this.RaisePropertyChangedEvent("Shadow5StationID");
            }
        }

        //Button
        public DelegateCommand AddParamCommand { get; private set; }
        public DelegateCommand DelParamCommand { get; private set; }
        public DelegateCommand SaveParamCommand { get; private set; }
        public DelegateCommand CanclEditParamCommand { get; private set; }
        public DelegateCommand NewVersionCommand { get; private set; }
        public DelegateCommand CopyCurrentVersionCommand { get; private set; }

        private ObservableCollection<CurrentLine> currentLines;
        private ObservableCollection<CurrentLine> tempLines;


        private CurrentLine defaultLine;
        private ObservableCollection<CurrentLine> currnetLinesSelected;

        [ImportingConstructor]
        public Para3085ViewModel()
        {
            currnetLinesSelected = new ObservableCollection<CurrentLine>();
            paramDao = new Para3085DAO();

            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
            if (paramDao.GetAllVersion().Count == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("当前参数无任何参数版本");
            }
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            EditGroupBoxVisible = Visibility.Hidden;

            currentLines = paramDao.GetCurrentLine();


            defaultLine = new CurrentLine();
            defaultLine.LineID = "00";
            defaultLine.LineNameCn = "未使用";

            //Button
            this.AddParamCommand = new DelegateCommand(this.AddParam);
            this.DelParamCommand = new DelegateCommand(this.DelParam);
            this.SaveParamCommand = new DelegateCommand(this.SaveParam);
            this.CanclEditParamCommand = new DelegateCommand(this.CanclEditParam);
            this.NewVersionCommand = new DelegateCommand(this.NewVersion);
            this.CopyCurrentVersionCommand = new DelegateCommand(this.CopyCurrentVersion);
        }

        void InfoVerList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (InfoVerList.CurrentItem != null)
            {
                ReflashDataGrid();
            }
        }

        void BaseScLineList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (BaseScLineList.CurrentItem != null)
            {
                tempLines = new ObservableCollection<CurrentLine>();
                CurrentLine currentLineSelected = BaseScLineList.CurrentItem as CurrentLine;
                BaseScList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentStation(currentLineSelected.LineID.Trim()));
                BaseScList.CurrentChanged += BaseScList_CurrentChanged;
                if (BaseScList.CurrentItem != null)
                {
                    BaseStationID = (BaseScList.CurrentItem as CurrentStation).StationID;
                }

                tempLines.Add(defaultLine);
                foreach (CurrentLine item in currentLines)
                {
                    if (!currentLineSelected.LineID.Equals(item.LineID))
                    {
                        tempLines.Add(item);
                    }
                }
                Shadow1ScLineList = CollectionViewSource.GetDefaultView(new ObservableCollection<CurrentLine>(tempLines));
                Shadow1ScLineList.CurrentChanged += Shadow1ScLineList_CurrentChanged;
                Shadow2ScLineList = CollectionViewSource.GetDefaultView(new ObservableCollection<CurrentLine>(tempLines));
                Shadow2ScLineList.CurrentChanged += Shadow2ScLineList_CurrentChanged;
                Shadow3ScLineList = CollectionViewSource.GetDefaultView(new ObservableCollection<CurrentLine>(tempLines));
                Shadow3ScLineList.CurrentChanged += Shadow3ScLineList_CurrentChanged;
                Shadow4ScLineList = CollectionViewSource.GetDefaultView(new ObservableCollection<CurrentLine>(tempLines));
                Shadow4ScLineList.CurrentChanged += Shadow4ScLineList_CurrentChanged;
                Shadow5ScLineList = CollectionViewSource.GetDefaultView(new ObservableCollection<CurrentLine>(tempLines));
                Shadow5ScLineList.CurrentChanged += Shadow5ScLineList_CurrentChanged;

            }
        }

        void Shadow1ScLineList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow1ScLineList.CurrentItem != null)
            {
                CurrentLine currentLineSelected = Shadow1ScLineList.CurrentItem as CurrentLine;
                Shadow1ScList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentStation(currentLineSelected.LineID.Trim()));
                Shadow1ScList.CurrentChanged += Shadow1ScList_CurrentChanged;
                if (Shadow1ScList.CurrentItem != null)
                {
                    Shadow1StationID = (Shadow1ScList.CurrentItem as CurrentStation).StationID;
                }
            }
        }

        void Shadow2ScLineList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow2ScLineList.CurrentItem != null)
            {
                CurrentLine currentLineSelected = Shadow2ScLineList.CurrentItem as CurrentLine;
                Shadow2ScList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentStation(currentLineSelected.LineID.Trim()));
                Shadow2ScList.CurrentChanged += Shadow2ScList_CurrentChanged;
                if (Shadow2ScList.CurrentItem != null)
                {
                    Shadow2StationID = (Shadow2ScList.CurrentItem as CurrentStation).StationID;
                }
            }
        }

        void Shadow3ScLineList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow3ScLineList.CurrentItem != null)
            {
                CurrentLine currentLineSelected = Shadow3ScLineList.CurrentItem as CurrentLine;
                Shadow3ScList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentStation(currentLineSelected.LineID.Trim()));
                Shadow3ScList.CurrentChanged += Shadow3ScList_CurrentChanged;
                if (Shadow3ScList.CurrentItem != null)
                {
                    Shadow3StationID = (Shadow3ScList.CurrentItem as CurrentStation).StationID;
                }
            }
        }

        void Shadow4ScLineList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow4ScLineList.CurrentItem != null)
            {
                CurrentLine currentLineSelected = Shadow4ScLineList.CurrentItem as CurrentLine;
                Shadow4ScList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentStation(currentLineSelected.LineID.Trim()));
                Shadow4ScList.CurrentChanged += Shadow4ScList_CurrentChanged;
                if (Shadow4ScList.CurrentItem != null)
                {
                    Shadow4StationID = (Shadow4ScList.CurrentItem as CurrentStation).StationID;
                }
            }
        }

        void Shadow5ScLineList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow5ScLineList.CurrentItem != null)
            {
                CurrentLine currentLineSelected = Shadow5ScLineList.CurrentItem as CurrentLine;
                Shadow5ScList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentStation(currentLineSelected.LineID.Trim()));
                Shadow5ScList.CurrentChanged += Shadow5ScList_CurrentChanged;
                if (Shadow5ScList.CurrentItem != null)
                {
                    Shadow5StationID = (Shadow5ScList.CurrentItem as CurrentStation).StationID;
                }
            }
        }

        void BaseScList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (BaseScList.CurrentItem != null)
            {
                BaseStationID = (BaseScList.CurrentItem as CurrentStation).StationID;
            }
        }

        //Shadow1ScList
        void Shadow1ScList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow1ScList.CurrentItem != null)
            {
                Shadow1StationID = (Shadow1ScList.CurrentItem as CurrentStation).StationID;
            }
        }

        void Shadow2ScList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow2ScList.CurrentItem != null)
            {
                Shadow2StationID = (Shadow2ScList.CurrentItem as CurrentStation).StationID;
            }
        }

        void Shadow3ScList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow3ScList.CurrentItem != null)
            {
                Shadow3StationID = (Shadow3ScList.CurrentItem as CurrentStation).StationID;
            }
        }

        void Shadow4ScList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow4ScList.CurrentItem != null)
            {
                Shadow4StationID = (Shadow4ScList.CurrentItem as CurrentStation).StationID;
            }
        }

        void Shadow5ScList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (Shadow5ScList.CurrentItem != null)
            {
                Shadow5StationID = (Shadow5ScList.CurrentItem as CurrentStation).StationID;
            }
        }

        private void ReflashDataGrid()
        {
            int infoVer = (int)InfoVerList.CurrentItem;
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(infoVer));
            BaseScLineList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentLine());
            BaseScLineList.CurrentChanged += BaseScLineList_CurrentChanged;
        }

        private void AddParam()
        {
            BaseScLineList = CollectionViewSource.GetDefaultView(paramDao.GetCurrentLine());
            BaseScLineList.CurrentChanged += BaseScLineList_CurrentChanged;
            if (InfoVerList.CurrentItem == null)
            {
                Microsoft.Windows.Controls.MessageBox.Show("请添加参数版本！才可进行添加");
            }
            else
            {
                EditGroupBoxVisible = Visibility.Visible;
            }
        }

        private void DelParam()
        {
            try
            {
                if (ParaCollectionDetail.CurrentItem != null)
                {
                    Para3085 param = ParaCollectionDetail.CurrentItem as Para3085;
                    if (Microsoft.Windows.Controls.MessageBox.Show("确认删除所选择的参数！", "3085参数", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
                    {
                        //删除
                        ResultValue result = paramDao.DeleteObject(param, param.para3085Pk.infoVer);
                        if (result.Code == 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除成功");
                            //刷新
                            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;

                            ReflashDataGrid();

                        }
                        else
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除失败!发生错误：" + result.Msg);
                        }
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("未选择要删除的参数");
                }
            }
            catch (Exception e)
            {
            }
        }

        public void SaveParam()
        {
            if (Confirm())
            {
                Para3085 param = new Para3085();
                Para3085Pk paramPk = new Para3085Pk();
                int tmp = (int)InfoVerList.CurrentItem;
                paramPk.infoVer = Int16.Parse(tmp.ToString());
                paramPk.baseSc = BaseStationID;

                param.para3085Pk = paramPk;
                param.shadowSc1 = Shadow1StationID;
                param.shadowSc2 = Shadow2StationID;
                param.shadowSc3 = Shadow3StationID;
                param.shadowSc4 = Shadow4StationID;
                param.shadowSc5 = Shadow5StationID;
                //编辑参数
                ResultValue result = paramDao.CreateObject(param);
                if (result.Code == 0)
                {
                    CanclEditParam();
                    Microsoft.Windows.Controls.MessageBox.Show("保存成功");
                    ReflashDataGrid();

                    
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result.Msg);
                }
            }

        }

        private void CanclEditParam()
        {
            this.EditGroupBoxVisible = Visibility.Hidden;
            //将控件置为空
            /*
            BaseStationID = null;
            Shadow1StationID = null;
            Shadow2StationID = null;
            Shadow3StationID = null;
            Shadow4StationID = null;
            Shadow5StationID = null;
             */
        }

        private bool Confirm()
        {
            bool flag = true;
            /*
            if (this.LineID != null && !"".Equals(this.LineID) && this.LineNmCn != null && !"".Equals(this.LineNmCn) && !"".Equals(this.LineNmEn) && this.LineNmEn != null)
            {
                flag = true;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所填信息不完整");
                flag = false;
            }
             */
            return flag;
        }

        private void NewVersion()
        {
            ObservableCollection<int> infoVerCollection = new ObservableCollection<int>();
            infoVerCollection = paramDao.GetAllVersion();
            int newVersion = paramDao.GetMaxID() + 1;
            infoVerCollection.Add(newVersion);
            InfoVerList = CollectionViewSource.GetDefaultView(infoVerCollection);
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
        }

        private void CopyCurrentVersion()
        {
            //查询当前版本
            ObservableCollection<Para3085> currentVersionParam = new ObservableCollection<Para3085>();
            int infoVer = (int)InfoVerList.CurrentItem;
            currentVersionParam = paramDao.GetObjectByInfoVer(infoVer);

            if (currentVersionParam.Count != 0)
            {
                Para3085 param = null;
                Para3085Pk paramPk = null;
                int newVersion = paramDao.GetMaxID() + 1;
                foreach (Para3085 item in currentVersionParam)
                {
                    param = new Para3085();
                    paramPk = new Para3085Pk();
                    //编辑参数
                    paramPk.infoVer = Int16.Parse(newVersion.ToString());
                    paramPk.baseSc = item.para3085Pk.baseSc;

                    param.para3085Pk = paramPk;
                    param.shadowSc1 = item.shadowSc1;
                    param.shadowSc2 = item.shadowSc2;
                    param.shadowSc3 = item.shadowSc3;
                    param.shadowSc4 = item.shadowSc4;
                    param.shadowSc5 = item.shadowSc5;

                    ResultValue result = paramDao.CreateObject(param);
                    if (result.Code != 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("复制过程中出现错误!错误信息：" + result.Msg);
                    }
                }
                //刷新
                ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(newVersion));
                InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所查询的当前版本没有数据,不可进行复制");
            }
        }
    }
}
